﻿using System;
using System.Collections.Generic;
using System.Text; 
using MongoDB;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver.GeoJsonObjectModel;
using System.Linq;

namespace AppSocketModel
{
    /// <summary>
    /// 围栏
    /// </summary>
    [BsonIgnoreExtraElements]
    public class FenceDto
    {
        /// <summary>
        /// Id
        /// </summary>
        public string Id { set; get; }

        /// <summary>
        /// IMEI
        /// </summary>
        public string IMEI { set; get; }

        /// <summary>
        /// 公司Id
        /// </summary>
        public int CompanyId { set; get; }
        
        /// <summary>
        /// 账户Id
        /// </summary>
        public string AccountId { set; get; }

        /// <summary>
        /// 围栏名称
        /// </summary>
        public string Name { set; get; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsEnable { set; get; }

        /// <summary>
        /// 围栏编号
        /// </summary>
        public int FenceNumber { set; get; }

        /// <summary>
        /// 电子围栏报警类型
        /// </summary>
        public FenceAlarmType FenceAlarmType { set; get; }


        /// <summary>
        /// 多边形范围
        /// </summary>          
        public List<Map2DCoordinate> CoordinateList
        {
            get
            {
                if(Polygon!=null && Polygon.Coordinates!=null && Polygon.Coordinates.Exterior!=null && Polygon.Coordinates.Exterior.Positions!=null && Polygon.Coordinates.Exterior.Positions.Count>0)
                {
                    List<Map2DCoordinate> list = new  List<Map2DCoordinate>(); 
                    foreach (var item in Polygon.Coordinates.Exterior.Positions)
                    {
                        list.Add(new Map2DCoordinate(item.Latitude, item.Longitude));
                    }
                    return list;
                }
                return null;
            }
            set
            { 
                if (value.Count < 4)
                    throw new ArgumentException("围栏不能小于4个点!");

                if (value[0].lat != value[value.Count - 1].lat
                    || value[0].lng != value[value.Count - 1].lng)
                    throw new ArgumentException("围栏第一个点必须和最后一个点重合!");

                var list = new List<GeoJson2DGeographicCoordinates>();
                foreach (var item in value)
                {
                    list.Add(new GeoJson2DGeographicCoordinates(item.lng, item.lat));
                } 
                try
                {
                    SetPolygon(list);
                }
                catch (System.Exception ex)
                {
                    throw new ArgumentException(ex.Message);
                }

            }
        }


        /// <summary>
        /// 多边形范围
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public  GeoJsonPolygon<GeoJson2DGeographicCoordinates> Polygon { get;  set; }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="coordinatesList"></param>
        public void SetPolygon(List<GeoJson2DGeographicCoordinates> coordinatesList)
        {
            Polygon = new GeoJsonPolygon<GeoJson2DGeographicCoordinates>(
                                new GeoJsonPolygonCoordinates<GeoJson2DGeographicCoordinates>(
                                    new GeoJsonLinearRingCoordinates<GeoJson2DGeographicCoordinates>(
                                        coordinatesList)));
        }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateDateTime { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime UpdateDateTime { get; set; }

        /// <summary>
        /// 修改人角色
        /// </summary>
        public string UpdateAccountRole { get; set; }

        /// <summary>
        /// 修改人账户
        /// </summary>
        public string UpdateAccountCode { get; set; }
    }
     
}
